package com.zkingsoft.web.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.web.bean.WebArticleType;
import com.zkingsoft.web.service.WebArticleTypeService;

/**
 * @author 陈才
 * @date 2016-12-01 10:36
 */
@Controller
@RequestMapping(value = "admin/webArticleType")
public class WebArticleTypeController {

	@Resource
	private WebArticleTypeService webArticleTypeService;
	 
	
	
	//记录编辑前的值Before_Edit_Value
	public static final String BEV="WebArticleType_BEV";
	 
	
	/**
	 * 列表显示
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(WebArticleType webArticleType, PaginationVO pageVo) {
		if (pageVo == null) {
			pageVo = new PaginationVO();
		}
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		webArticleType.setCompanyId(user.getCompanyId());
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS,  
				webArticleTypeService.findInPage(webArticleType, pageVo), webArticleTypeService.findTotal(webArticleType));
		return result;
	}
	/**
	 * 加载所有列表
	 */
	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all(WebArticleType webArticleType) {
		SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		webArticleType.setCompanyId(user.getCompanyId());
		List<WebArticleType> list = webArticleTypeService.findByModel(webArticleType);
		if(webArticleType.getArtTypeId() != null && list.size()>0){
			WebUtil.setSessionAttribute("WebArticleType_BEV",list.get(0));
		}
		return new AjaxResult(AjaxResult.STATUS_SUCCESS,list	, 0);
		
	}
   	
	/**
	 * 新增或者修改页面
	 */   
   	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(WebArticleType webArticleType) {
		if (webArticleType.getArtTypeId() != null) {
			int i = webArticleTypeService.modifyByMap(WebUtil.getSessionAttribute(BEV), webArticleType);
			WebUtil.removeSessionAttribute(BEV);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_SUCCESS,  "文章类型修改成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_FAIL,  "文章类型修改失败");
			}
		} else {
			SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
			webArticleType.setCompanyId(user.getCompanyId());
			int i = webArticleTypeService.add(webArticleType);
			if (i > 0) {
				return new AjaxResult(AjaxResult.STATUS_SUCCESS,  "文章类型新增成功");
			} else {
				return new AjaxResult(AjaxResult.STATUS_FAIL,  "文章类型新增失败");
			}
		}
	}
	
   	/**
	 * 进入修改界面
	 */   
   	@RequestMapping(value = "/editForm")
	public String editForm(Long id) {
		WebArticleType webArticleType;
		if (id != null) {
			webArticleType = webArticleTypeService.findById(id);
			WebUtil.getRequest().setAttribute("obj", webArticleType);
			WebUtil.setSessionAttribute(BEV, webArticleType);
		}
		return "admin/web/articleType-form";
	}
   	
   	
   	/**
	 * 删除
 */
 	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
 		List<Long> ids = StringUtils.strToCollToLong(keys, ",");
		int i = webArticleTypeService.remove(ids);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS,  "成功删除" + i + "条数据");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL,  "删除失败");
		}
	}
	
	/**
	 * 删除文章类型及其子类
	 */  
 	@RequestMapping(value = "/delAll")
	public @ResponseBody AjaxResult delAll(Long artTypeId) {
		int i = webArticleTypeService.removeById(artTypeId);
		if (i > 0) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS,  "成功删除" + i + "条数据");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL,  "删除失败");
		}
	}
}